package com.liuxing.javaSpringBoot.modules.account.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liuxing.javaSpringBoot.modules.account.dao.ResourceDao;
import com.liuxing.javaSpringBoot.modules.account.dao.RoleResourceDao;
import com.liuxing.javaSpringBoot.modules.account.entity.Resource;
import com.liuxing.javaSpringBoot.modules.account.service.ResourceService;
import com.liuxing.javaSpringBoot.modules.common.entity.Result;
import com.liuxing.javaSpringBoot.modules.common.entity.SearchBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * Created by IntelliJ IDEA.
 * @File ResourceServiceImpl.java
 * @Auth liuxing
 * @Date 2021/8/23 17:48
 * @Email liuxing997@foxmail.com
 */
@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private ResourceDao resourceDao;

    @Autowired
    private RoleResourceDao roleResourceDao;

    //插入资源信息
    @Override
    @Transactional
    public Result<Resource> insertResource(Resource resource) {
        resource.setCreateDate(LocalDateTime.now());
        resourceDao.insertResource(resource);
        return new Result<Resource> (Result.ResultCode.SUCCESS.code,
        "Insert success.",resource);
    }

    //根据资源ID修改资源信息
    @Override
    @Transactional
    public Result<Resource> updateResourceById(Resource resource) {
        resourceDao.updateResourceById(resource);
        return new Result<Resource>(Result.ResultCode.SUCCESS.code,"Update success.",resource);
    }

    //根据资源ID删除资源信息
    @Override
    @Transactional
    public Result<Object> deleteResourceByResourceId(int id) {
        resourceDao.deleteResourceByResourceId(id);
        roleResourceDao.deleteRoleResourceByResourceId(id);
        return new Result<>(Result.ResultCode.SUCCESS.code,
                "Delete success.");
    }

    //根据资源ID查询资源信息
    @Override
    public Resource getResourceByResourceId(int id) {
        return resourceDao.getResourceByResourceId(id);
    }

    //查询所有资源信息
    @Override
    public List<Resource> getAllResource() {
        return Optional
                .ofNullable(resourceDao.getAllResource())
                .orElse(Collections.emptyList());
    }

    //根据角色ID查询资源信息
    @Override
    public List<Resource> getResourcesByRoleId(int roleId) {
        return resourceDao.getResourcesByRoleId(roleId);
    }

    //批量插入资源信息
    @Override
    @Transactional
    public Result<Object> insertResources(List<Resource> resources) {
        resources.stream().forEach(item ->{
            item.setCreateDate(LocalDateTime.now());
        });
        resourceDao.insertResources(resources);
        return new Result<>(Result.ResultCode.SUCCESS.code,
                "Insert resources success.");
    }

    //分页查询资源信息
    @Override
    public PageInfo<Resource> getResourcesBySearchBean(SearchBean searchBean) {
        searchBean.initSearchBean();
        PageHelper.startPage(searchBean.getCurrentPage(),searchBean.getPageSize());
        return new  PageInfo<> (Optional
                .ofNullable(resourceDao.getResourcesBySearchBean(searchBean))
                .orElse(Collections.emptyList()));
    }
}
